package com.javaee.tallybook.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class PageController {

    /**
     * 安全
     * @param session
     * @return
     */
    @GetMapping("/index.html")
    public ModelAndView index(
            HttpSession session
    ){
        Object name = session.getAttribute("name");
        if (name!=null){
            return new ModelAndView("redirect:/index.html");
        }else {
            return new ModelAndView("redirect:/login.html");
        }
    }

    /**
     * 登出，删除session内容并返回登录页
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public ModelAndView logou(
            HttpSession session
    ){
        session.removeAttribute("name");
        return new ModelAndView("redirect:/login.html");
    }
}
